package com.netty.nio;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 写出到本地
 */
public class NioFileChannel01 {
    public static void main(String[] args) throws IOException {
        String str = "hello netty nio-channel";
        //创建输出流
        FileOutputStream filrOutputString = new FileOutputStream("E:\\fileout.txt");
        //通过fileOutputStream 获取对应的fileChannel
        FileChannel fileChannel = filrOutputString.getChannel();
        //创建一个缓冲区byteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        //将str放入到byteBuffer
        byteBuffer.put(str.getBytes());
        //对byteBuffer进行flip(反转)
        byteBuffer.flip();

        //将byteBuffer的数据写入到Channel里面,byteBuffer-->Channel
        fileChannel.write(byteBuffer);
        filrOutputString.close();
    }
}